package com.topscore.uke.main;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.kingja.loadsir.core.LoadSir;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.topscore.uke.database.entities.SearchHistoryItem;
import com.topscore.uke.entities.UserInfo;
import com.topscore.uke.ui.loadsir_callback.EmptyCallBack;
import com.topscore.uke.ui.loadsir_callback.ErrorCallBack;
import com.topscore.uke.ui.loadsir_callback.LoadingCallBack;
import com.topscore.uke.ui.loadsir_callback.TimeOutCallBack;
import com.topscore.uke.utils.StringUtil;
import com.topscore.uke.utils.UserInfoManager;
import com.topscore.utillibrary.database.DataBaseHelper;

import java.io.File;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by 71468 on 2017/9/12.
 */

public class BaseApplication extends Application {

    private static SharedPreferences mSharePreferences;
    private static DataBaseHelper dataBaseHelper;
    private static Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        initAppData();
        initImageLoader(this);
        initUserInfo();
        initLoadSir();
        initJPush();//开启极光推送
//        initUMengSocialize();
        initWechatSdk();
        setDataBaseHelper(DataBaseHelper.getInstance(this, "uke", 1, new Class[]{SearchHistoryItem.class}));
    }

    public static Context getContext() {
        return mContext;
    }

    public static void setDataBaseHelper(DataBaseHelper dataBaseHelper) {
        BaseApplication.dataBaseHelper = dataBaseHelper;
    }

//    private void initUMengSocialize() {
//        UMConfigure.init(this, "5abb64688f4a9d0229000029", "umeng", UMConfigure.DEVICE_TYPE_PHONE, null);
//        Config.DEBUG = true;
//        PlatformConfig.setWeixin("wx03192d0b29e18848","e0ca1b46fb7e8de72ca201c54f82cf0d");
//    }

    private void initWechatSdk() {
        IWXAPI iwxapi = WXAPIFactory.createWXAPI(this,"wx03192d0b29e18848");
        iwxapi.registerApp("wx03192d0b29e18848");
    }

    public static DataBaseHelper getDataBaseHelper() {
        return dataBaseHelper;
    }

    private void initJPush() {
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
    }

    private void initUserInfo() {
        String info = getString(Contracts._UserInfo);
        if (!StringUtil.isEmpty(info)) {
            UserInfo userInfo = new Gson().fromJson(info, UserInfo.class);
            UserInfoManager.setUserInfo(userInfo);
        } else {
            UserInfoManager.setUserInfo(null);
        }
    }

    private void initLoadSir() {
        LoadSir.beginBuilder()
                .addCallback(new LoadingCallBack())
                .addCallback(new ErrorCallBack())
                .addCallback(new TimeOutCallBack())
                .addCallback(new EmptyCallBack())
                .setDefaultCallback(LoadingCallBack.class)
                .commit();
    }

    private void initImageLoader(Context context) {
        File cacheDir = StorageUtils.getOwnCacheDirectory(context, "uke/images");
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration
                .Builder(this)
                .memoryCacheExtraOptions(480, 800)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .threadPoolSize(3)
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .diskCache(new UnlimitedDiscCache(cacheDir))
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.FIFO)
                .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
                .memoryCacheSize((int) (Runtime.getRuntime().maxMemory() / 8))
                .imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000))
                .writeDebugLogs()
                .build();
        ImageLoader.getInstance().init(configuration);
    }

    private void initAppData() {
        mSharePreferences = this.getSharedPreferences("mApp", MODE_PRIVATE);
    }

    public synchronized static void put(String key, String value) {
        synchronized (mSharePreferences) {
            mSharePreferences.edit().putString(key, value).commit();
        }
    }

    public static void put(String key, boolean value) {
        synchronized (mSharePreferences) {
            mSharePreferences.edit().putBoolean(key, value).commit();
        }
    }

    public static void put(String key, int type) {
        synchronized (mSharePreferences) {
            mSharePreferences.edit().putInt(key, type).commit();
        }
    }

    public static String getString(String key) {
        synchronized (mSharePreferences) {
            return mSharePreferences.getString(key, "");
        }
    }

    public static boolean getBoolean(String key) {
        synchronized (mSharePreferences) {
            return mSharePreferences.getBoolean(key, false);
        }
    }

    public static boolean getBoolean(String key, boolean defaultVaule) {
        synchronized (mSharePreferences) {
            return mSharePreferences.getBoolean(key, defaultVaule);
        }
    }

    public static long getLong(String key, long defaultVaule) {
        synchronized (mSharePreferences) {
            return mSharePreferences.getLong(key, defaultVaule);
        }
    }

    public static int getInt(String key, int defaultVaule) {
        synchronized (mSharePreferences) {
            return mSharePreferences.getInt(key, defaultVaule);
        }
    }

    public static float getFloat(String key, float defaultVaule) {
        synchronized (mSharePreferences) {
            return mSharePreferences.getFloat(key, defaultVaule);
        }
    }

    public static void cleanCach(String key) {
        synchronized (mSharePreferences) {
            mSharePreferences.edit().remove(key).commit();
        }
    }
}
